<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <page-filter
      v-model:query="filterInfo.query"
      :filter-list="filterInfo.list"
    >
      <template v-slot:filter-keyword>
        <div class="scan-groups">
          <el-input
            v-model="filterInfo.keyword"
            :clearable="true"
            placeholder="请输入名称"
            @keyup.enter.native="search"
            @input="(e) => (filterInfo.keyword = validSe(e))"
          >
          </el-input>
        </div> </template
    ></page-filter>
    <dialog-table
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      :listen-height="false"
      :tab-index="tabIndex"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :check-box="checkBox"
      :checkedList="checkBox ? checkedList : []"
      :pager="data && data.length ? false : true"
      @handleEvent="handleEvent"
    />
  </page-dialog>
</template>

<script setup name="selectContractNoCustomize">
import PageDialog from '../PageDialog/index'
import PageFilter from '../PageFilter/index'
import DialogTable from '@/components/DialogTable/index'
import { getContractPage } from '@/api/contract'
const { proxy } = getCurrentInstance()
const emit = defineEmits(['update', 'callback', 'reset'])
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: true,
  },
  checkBox: {
    type: Boolean,
    default: false,
  },
  tabIndex: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getContractPage,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  checkedList: {
    type: Array,
    default: function () {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
  data: {
    type: Array,
    default: function () {
      return []
    },
  },
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    sort: 'code asc',
    ...props.query,
  },
  list: [
    {
      type: 'slot',
      label: '名称',
      value: 'keyword',
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  copyData: [],
  fieldList: [
    { label: '合同编号', value: 'orderNo' },
    { label: '合同名称', value: 'contractName' },
    { label: '状态', value: 'statusValue' },
    { label: '合同生效日期', value: 'contractStartDate' },
    { label: '合同失效日期', value: 'contractEndDate' },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: '切换合同',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')
const shows = computed({
    get() {
      return props.showDialog
    },
    set(newValue) {
      emit('update:showDialog', newValue) // 更新父组件shows
    }
})
// 获取列表
function getList() {
  tableInfo.value.api = props.api
  tableInfo.value.refresh = Math.random()
}
function search() {
  if (props.data && props.data.length) {
    const search = filterInfo.value.keyword
    if (search) {
      tableInfo.value.data = tableInfo.value.copyData.filter((data) => {
        return Object.keys(data).some((key) => {
          if (key === 'orderNo') {
            return String(data[key]).indexOf(search) > -1
          } else if (key === 'contractName') {
            return String(data[key]).indexOf(search) > -1
          }
        })
      })
    } else {
      tableInfo.value.data = tableInfo.value.copyData
    }
  } else {
    tableInfo.value.initCurpage = Math.random()
    tableInfo.value.refresh = Math.random()
  }
}
function save() {
  handleClose()
  emit('callback', selectRow.value)
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
    case 'tableCheck':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emit('reset')
}
onMounted(() => {
  // hack, 在table创建出来之后再去调用
  proxy.$nextTick(() => {
    if (props.data && props.data.length) {
      //父组件传的列表数据
      tableInfo.value.data = props.data
      tableInfo.value.copyData = proxy.$fn.deepClone(props.data)
    } else {
      getList()
    }
  })
})
</script>

<style scoped></style>
